package nl.hanze.web.homegrownrpc.hello;

import nl.hanze.web.homegrownrpc.generic.*;
import java.net.*;
import java.io.*;
import sun.misc.*;

public class HelloSkel implements Skel<Hello> {
    private int port;
    private Hello hello;
    private ServerSocket ssoHelloSkel;
	
    public void setPort(int port) {
        this.port=port;
    }

    public void setImplementation(Hello hello) {
        this.hello = hello;
    }

    public void listen() throws Exception {
        ssoHelloSkel = new ServerSocket(port);

        while(true) {
            Socket socHelloSkel=ssoHelloSkel.accept();
            OutputStreamWriter oswHelloSkel=new OutputStreamWriter(socHelloSkel.getOutputStream());
            BufferedReader bufHelloSkel=new BufferedReader(new InputStreamReader(socHelloSkel.getInputStream()));
            String strMethod=bufHelloSkel.readLine();

            if (strMethod.startsWith("sayHello#0")) {
                String value=new BASE64Encoder().encode(hello.sayHello().getBytes());
                oswHelloSkel.write("java.lang.String#"+value+"\n");
                oswHelloSkel.flush();
                continue;
            }

            if (strMethod.startsWith("sayHello#1#java.lang.String")) {
                String param1=strMethod.substring("sayHello#1#java.lang.String".length()+1);
                param1=new String(new BASE64Decoder().decodeBuffer(param1));
                String value=new BASE64Encoder().encode(hello.sayHello(param1).getBytes());
                oswHelloSkel.write("java.lang.String#"+value+"\n");
                oswHelloSkel.flush();
                continue;
            }

            if (strMethod.startsWith("sayHello#1#int")) {
                // Get the argument for sayHello(int age)
                int param1 = Integer.parseInt(strMethod.substring("sayHello#1#int#".length()));
                // Run the method and encode the result with BASE64
                String value = new BASE64Encoder().encode(hello.sayHello(param1).getBytes());
                // Send the result to the requester over the open socket
                oswHelloSkel.write("java.lang.String#"+value+"\n");
                oswHelloSkel.flush();
                continue;
            }

            if (strMethod.startsWith("sayHello#2#java.lang.String#int")) {
                String[] param=strMethod.substring("sayHello#2#java.lang.String#int".length()+1).split("#");
                String param1=new String(new BASE64Decoder().decodeBuffer(param[0]));
                int param2=Integer.parseInt(param[1]);
                String value=new BASE64Encoder().encode(hello.sayHello(param1, param2).getBytes());
                oswHelloSkel.write("java.lang.String#"+value+"\n");
                oswHelloSkel.flush();
                continue;
            }

            if (strMethod.startsWith("ageNextYear#1#int")) {
                int param1=Integer.parseInt(strMethod.substring("ageNextYear#1#int".length()+1));
                int value=hello.ageNextYear(param1);
                oswHelloSkel.write("int#"+value+"\n");
                oswHelloSkel.flush();
                continue;
            }
        }
    }
}